package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.config.AutoLog;
import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.SysArea;
import com.blb.zycommunity.entity.ZyBuilding;
import com.blb.zycommunity.entity.ZyCommunity;
import com.blb.zycommunity.service.IZyBuildingService;
import com.blb.zycommunity.service.IZyCommunityService;
import com.blb.zycommunity.utils.ExcelDownloadUtil;
import com.blb.zycommunity.utils.TreeUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 楼栋  前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@CrossOrigin
@RestController
public class ZyBuildingController {

    @Resource
    private IZyBuildingService zyBuildingService;

    @Resource
    private IZyCommunityService zyCommunityService;

    @GetMapping("/building-pages")
    public ResponseResult<List<ZyBuilding>> findBuildingPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                             @RequestParam(required = false,defaultValue = "5") Long size,
                                                             @RequestParam(required = false,defaultValue = "") String buildingName,
                                                             @RequestParam(required = false,defaultValue = "") String buildingCode){
        Page<ZyBuilding> zyBuildingPage = new Page<>(current, size);
        IPage<ZyBuilding> buildingPage = zyBuildingService.findBuildingPage(zyBuildingPage, buildingName, buildingCode);
        return ResponseResult.ok(buildingPage);
    }

    @AutoLog(value = 1,title = "楼栋新增")
    @PostMapping("/building-add")
    public ResponseResult addBuilding(@RequestBody ZyBuilding building){
        boolean save = zyBuildingService.save(building);
        return ResponseResult.ok(save);
    }

    @AutoLog(value = 2,title = "楼栋修改")
    @PutMapping("/building-edit")
    public ResponseResult editBuilding(@RequestBody ZyBuilding building){
        boolean update = zyBuildingService.updateById(building);
        return ResponseResult.ok(update);
    }

    @AutoLog(value = 3,title = "楼栋删除")
    @DeleteMapping("/building-delete/{id}")
    public ResponseResult deleteBuilding(@PathVariable String id){
        boolean removeById = zyBuildingService.removeById(id);
        return ResponseResult.ok(removeById);
    }

    @DeleteMapping("/building-del/{buildingIds}")
    public ResponseResult deleteBuildingByIds(@PathVariable Long[] buildingIds){
        for (Long buildingId : buildingIds) {
            zyBuildingService.removeById(buildingId);
        }
        return ResponseResult.ok("ok");
    }


    @GetMapping("/community-get")
    public ResponseResult<List<ZyCommunity>> findAreaAll(){
        List<ZyCommunity> community = zyCommunityService.findAllCommunity();
        return ResponseResult.ok(community);
    }

    /**
     * 根据id导出数据
     * @param response
     * @param ids
     * @param filename
     * @throws IOException
     */
    @AutoLog(value = 6,title = "楼栋导出")
    @GetMapping("downloads-building")
    public void exports(
            HttpServletResponse response,
            @RequestParam("ids") long[] ids,
            @RequestParam("filename") String filename) throws IOException {
        System.out.println(filename);
        List<ZyBuilding> zyBuildings = new ArrayList<>();
        for (Long id : ids) {
            String idstr = id.toString();
            ZyBuilding zyBuilding = zyBuildingService.findBuildingById(idstr);
            zyBuildings.add(zyBuilding);
        }
        ExcelDownloadUtil.excelDownload(response, filename, ZyBuilding.class, zyBuildings);
    }
}
